package www.bancodelanacion.intranet.prestamos.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import www.bancodelanacion.intranet.prestamos.entidad.IfiDTO;
import www.bancodelanacion.intranet.prestamos.util.MySqlDBConn;

public class IfiDAOImpl implements IfiDAO{

	@Override
	public IfiDTO buscaIFIporcodigo(String codigo) throws SQLException {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		IfiDTO dto = new IfiDTO();
		try {
				conn = new MySqlDBConn().getConnection();
				String sql ="select * from tb_ifi where Codigo_Ifi=?";
				pstm = conn.prepareStatement(sql);
				pstm.setString(1, codigo);
				rs = pstm.executeQuery();
				if(rs.next()){
					dto.setTipo(rs.getString("Tipo"));
				}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(rs!= null)rs.close();
				if(pstm!= null)pstm.close();
				if(conn!= null)conn.close();
			} catch (Exception e2) {}
		}
		return dto;
	}

	@Override
	public IfiDTO buscaIFIportipo(String tipo) throws SQLException {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		IfiDTO dto = new IfiDTO();
		try {
				conn = new MySqlDBConn().getConnection();
				String sql ="select * from tb_ifi where Tipo=?";
				pstm = conn.prepareStatement(sql);
				pstm.setString(1, tipo);
				rs = pstm.executeQuery();
				if(rs.next()){
					dto.setCodigo(rs.getString("Codigo_Ifi"));
				}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(rs!= null)rs.close();
				if(pstm!= null)pstm.close();
				if(conn!= null)conn.close();
			} catch (Exception e2) {}
		}
		return dto;
	}

	@Override
	public ArrayList<IfiDTO> listaIfi() throws SQLException {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		ArrayList<IfiDTO> data = new ArrayList<IfiDTO>();
		try {
				conn = new MySqlDBConn().getConnection();
				String sql ="select * from tb_ifi";
				pstm = conn.prepareStatement(sql);
				rs = pstm.executeQuery();
				IfiDTO dto = null;
				while(rs.next()){
					dto = new IfiDTO();
					dto.setCodigo(rs.getString("Codigo_Ifi"));
					dto.setTipo(rs.getString("Tipo"));
					data.add(dto);
				}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(rs!= null)rs.close();
				if(pstm!= null)pstm.close();
				if(conn!= null)conn.close();
			} catch (Exception e2) {}
		}
		return data;
	}

	
	
}
